package com.bodacn.keypoint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class ImagePointLayout extends FrameLayout implements View.OnClickListener {

    ArrayList<PointPlace> points;

    FrameLayout layouPoints;

    ImageView imgBg;

    Context mContext;

    public ImagePointLayout(Context context) {
        this(context, null);
    }

    public ImagePointLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImagePointLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context, attrs);
    }


    private void initView(Context context, AttributeSet attrs) {
        mContext = context;
        View imgPointLayout = inflate(context, R.layout.layout_imgpoint_view, this);
        imgBg =  imgPointLayout.findViewById(R.id.imgBg);
        layouPoints = imgPointLayout.findViewById(R.id.layouPoints);
    }

    public void setImgBg(Bitmap bitmap) {
        imgBg.setImageBitmap(bitmap);
        addPoints(imgBg.getWidth(), imgBg.getHeight());
    }

    public void setPoints(ArrayList<PointPlace> points) {
        this.points = points;
        addPoints(imgBg.getWidth(), imgBg.getHeight());
    }

    private void addPoints(int width, int height) {
        layouPoints.removeAllViews();
        if (points!=null)
        for (int i = 0; i < points.size(); i++) {
            double width_scale = points.get(i).width_scale;
            double height_scale = points.get(i).height_scale;
            LinearLayout view = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_imgpoint_point, this, false);
            ImageView imageView = view.findViewById(R.id.imgPoint);
            imageView.setTag(i);
            AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
            animationDrawable.start();
            LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
            layoutParams.leftMargin = (int) (width * width_scale);
            layoutParams.topMargin = (int) (height * height_scale);
            imageView.setOnClickListener(this);
            view.setFocusable(false);
            view.setFocusableInTouchMode(false);
            imageView.setFocusable(false);
            imageView.setFocusableInTouchMode(false);
            layouPoints.setFocusable(false);
            layouPoints.setFocusableInTouchMode(false);
            layouPoints.addView(view, layoutParams);
        }
    }


    @Override
    public void onClick(View view) {
        int pos = (int) view.getTag();
        //Toast.makeText(getContext(), "pos : " + pos, Toast.LENGTH_SHORT).show();
    }
}
